package com.hmall.search.listener;

import com.hmall.common.constatnts.MqConstants;
import com.hmall.search.service.ISearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HmallListener {

    @Autowired
    private ISearchService searchService;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(MqConstants.HMALL_INSERT_QUEUE),
                    exchange = @Exchange(value = MqConstants.HMALL_EXCHANGE, type = ExchangeTypes.DIRECT),
                    key = MqConstants.HMALL_INSERT_KEY
            )
    )
    public void saveOrUpdateEs(String json) {
        searchService.saveOrUpdateEs(json);
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(MqConstants.HMALL_DELETE_QUEUE),
                    exchange = @Exchange(value = MqConstants.HMALL_EXCHANGE, type = ExchangeTypes.DIRECT),
                    key = MqConstants.HMALL_DELETE_KEY
            )
    )
    public void deleteEs(String json) {
        searchService.deleteEs(json);
    }
}